package cn.tworice.admin.service.impl;

import cn.tworice.admin.dao.ResourcesDao;
import cn.tworice.admin.dao.SystemDao;
import cn.tworice.admin.service.ResourcesService;
import cn.tworice.common.entity.Resources;
import cn.tworice.common.entity.dto.ResourcesDto;
import cn.tworice.common.utils.CodeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ResourcesServiceImpl implements ResourcesService {
    @Autowired
    private ResourcesDao dao;

    @Autowired
    private CodeUtil codeUtil;

    /**
     * 添加或更新资源
     * @author 二饭
     */
    @Override
    public int editResource(Resources resources, boolean router) {
        int i=0;
        if(resources.getName()==null || resources.getName().isEmpty() || resources.getPid()==null || resources.getSort()==null || resources.getType()==null){
            return i;
        }
        if (resources.getId() == null || resources.getId() == 0) {
            i=dao.insertResource(resources);
            if(router) {
                codeUtil.addRouter(resources.getType(), resources.getUrl(), resources.getName());
            }
        } else {
            i=dao.updateResource(resources);
        }
        return i;
    }

    /**
     * 批量删除资源
     * @author 二饭
     */
    @Override
    public int delResources(Integer[] ids) {
        List<Integer> list = List.of(ids);
        return dao.delResources(list);
    }

    /**
     * 通过条件查询资源
     * @author 二饭
     * @email 1786705138@qq.com
     */
    @Override
    public List<ResourcesDto> selectResources(String name) {
        // TODO 通过资源名称查询资源
//        List<ResourcesDto> list=dao.queryResources(name);
        return null;
    }
}
